package com.example.dict.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.textclassifier.ConversationAction;

import com.example.dict.bean.PinyinBushouWordBean;
import com.example.dict.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

public class DBManager {

    private static SQLiteDatabase db;
    public static void initDB(Context context){
        DBOpenHelper helper = new DBOpenHelper(context);
        db = helper.getWritableDatabase();
    }

//    执行插入数据到pyWordTb表当中，插入一个对象的方法
    public static void insertWordToPyWordTb(PinyinBushouWordBean.ResultBean.ListBean bean){
        ContentValues values = new ContentValues();
        values.put("id",bean.getId());
        values.put("zi",bean.getZi());
        values.put("py",bean.getPy());
        values.put("wubi",bean.getWubi());
        values.put("pinyin",bean.getPinyin());
        values.put("bushou",bean.getBushou());
        values.put("bihua",bean.getBihua());

        db.insert(CommonUtils.TABLE_PYWORDTB,null,values);
    }

//    插入数据到pyWordTb表当中，插入多个对象所在的集合
    public static void insertListToPyWordTb(List<PinyinBushouWordBean.ResultBean.ListBean> list){
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                //获取集合当中的每一个bean对象
                PinyinBushouWordBean.ResultBean.ListBean bean = list.get(i);
                //调用单个对象插入的方法
                try {
                    insertWordToPyWordTb(bean);
                }catch (Exception e){
                    Log.i("animee", "insertListToPyWordTb: "+bean.getZi()+"已经存在！");
                }
            }
        }
    }

//    查询pyWordTb表中指定拼音的数据
    public static List<PinyinBushouWordBean.ResultBean.ListBean> queryPyWordFrompyWordTb(String py,int page,int pagesize){
        List<PinyinBushouWordBean.ResultBean.ListBean> list = new ArrayList<>();
        String sql = "select * from pyWordTb where py=? limit ?,?";
        int start = (page-1) * pagesize;
        int end  = page * pagesize;
        Cursor cursor = db.rawQuery(sql, new String[]{py, start + "", end + ""});
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndexOrThrow("id"));
            String zi = cursor.getString(cursor.getColumnIndexOrThrow("zi"));
            String py1 = cursor.getString(cursor.getColumnIndexOrThrow("py"));
            String wubi = cursor.getString(cursor.getColumnIndexOrThrow("wubi"));
            String pinyin = cursor.getString(cursor.getColumnIndexOrThrow("pinyin"));
            String bushou = cursor.getString(cursor.getColumnIndexOrThrow("bushou"));
            String bihua = cursor.getString(cursor.getColumnIndexOrThrow("bihua"));

            PinyinBushouWordBean.ResultBean.ListBean bean = new PinyinBushouWordBean.ResultBean.ListBean(id, zi, py1, wubi, pinyin, bushou, bihua);
            list.add(bean);
        }
        return list;
    }

//    查询pyWordTb表中指定部首的数据
    public static List<PinyinBushouWordBean.ResultBean.ListBean> queryBsWordFrompyWordTb(String bs,int page,int pagesize){
        List<PinyinBushouWordBean.ResultBean.ListBean> list = new ArrayList<>();
        String sql = "select * from pyWordTb where bushou=? limit ?,?";
        int start = (page-1) * pagesize;
        int end  = page * pagesize;
        Cursor cursor = db.rawQuery(sql, new String[]{bs, start + "", end + ""});
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndexOrThrow("id"));
            String zi = cursor.getString(cursor.getColumnIndexOrThrow("zi"));
            String py1 = cursor.getString(cursor.getColumnIndexOrThrow("py"));
            String wubi = cursor.getString(cursor.getColumnIndexOrThrow("wubi"));
            String pinyin = cursor.getString(cursor.getColumnIndexOrThrow("pinyin"));
            String bushou = cursor.getString(cursor.getColumnIndexOrThrow("bushou"));
            String bihua = cursor.getString(cursor.getColumnIndexOrThrow("bihua"));

            PinyinBushouWordBean.ResultBean.ListBean bean = new PinyinBushouWordBean.ResultBean.ListBean(id, zi, py1, wubi, pinyin, bushou, bihua);
            list.add(bean);
        }
        return list;
    }
}
